<?php
	require_once ('scripts.php');
	require_once ('model/dao/vo/VehicleVO.php');
	require_once ('model/dao/vo/SubmissionVO.php');
	require_once ('model/dao/vo/ShareVO.php');
    require_once ('VehicleService.php');
	$action = $_REQUEST['action'];	
	if($action=='mediatorDetails')
	{     
		if($_POST['type']=='getSellerVehicle')
		{
			$vehicle_key =$_POST['key'];
            $result = VehicleDAOImpl::getInstance()->getBidVehicleDetails($vehicle_key);
            $data = encodeJsonStringMedaiator($result);
            echo  $data ;
		}
		else if($_POST['type']=='getPartnerVehicle')
		{
			$vehicle_key = $_REQUEST['key'];
            $result = VehicleDAOImpl::getInstance()->getShareDetails($vehicle_key);
            $data = encodeJsonStringMedaiator($result);
            echo  $data ;
		}
		else if($_POST['type']=='getUnknowndata')
		{
            $vehicle_key = $_REQUEST['key'];
            $result = VehicleDAOImpl::getInstance()->getUnknownShareDetails($vehicle_key);
            $data = encodeJsonStringMedaiator($result);
            echo  $data ;
		}
	}	
	if($action=='mediatoraddmessage')
	{
        $messageVO->messageVO = new MessageVO ();	
		$messageVO->message = $_POST['message'];
		$messageVO->messageOwener = $_POST['messageOwener'];
		$messageVO->messageRecipient = $_POST['messageRecipient'];
		$messageVO->vehicleId = $_POST['vehicleId'];
		$messageVO->userType = $_POST['userType'];
		$messageVO->messageTo = $_POST['messageTo'];
		$messageVO->offer = 'null';	
		$colName = 'update_date';
		$reply = $_POST['reply'];
		$doMail = false;
		$update='';			
		$result = VehicleDAOImpl::getInstance()->updateMessageVehicleID($messageVO, $colName, $reply, $doMail, $update);
		if($result)
		{
			$message = VehicleDAOImpl::getInstance()->getAllMessageVehicleID($_REQUEST['vehicleId']) ;
			$data = encodeJsonMessageString($message);
			echo $data;
		}
	}
  if($action=='gettowervbyid')
        {
            $vid=$_POST['veid'];
            $tower_id = $_POST['companyid'];
            $result = VehicleDAOImpl::getInstance()->getTowerVehicleByID($vid, $tower_id);
            if($result)
                $vehiclesingle=encodeJsonString($result);
            else
                $vehiclesingle=null;
                echo json_encode(array("vsingle"=>$vehiclesingle));
           
		
        }
			
	if($action=='getmonthlycount')
	{	
		$from = $_POST['from'];
		$to = $_POST['to'];
		$result = VehicleDAOImpl::getInstance()->monthlyCount($from,$to);	
		if(count($result->source)>0)
		{   
			$ar_weeks = $result->source;
			for($i=0; $i<count($ar_weeks); $i++)
			{  
			   $weeksarray[] = array(
				"nscount" => $ar_weeks[$i]->nscount,
				"npcount" => $ar_weeks[$i]->npcount,
				"bacount" => $ar_weeks[$i]->bacount
				);
			}
			$weekdata = array("weeks"=>$weeksarray);	
			echo json_encode($weekdata);		
		}
		else
		{
			echo 'false';
		}			      	
	}
	if($action=='getreminder')
	{
            
            $result= mysql_query("SELECT watchlist.watch_id,watchlist.vehicle_id,v.year,make.make,model.model_name,user.first_name,user.last_name,DATE(watchlist.set_date_time) as set_date,watchlist.to_date,watchlist.remind_note,(CASE WHEN to_date<now() THEN 1 ELSE 0 END)AS due, (CASE WHEN to_date BETWEEN now() and ADDDATE(now(),10) THEN 1 ELSE 0 END)AS duesoon  FROM watchlist LEFT JOIN  vehicle v  ON v.vehicle_id=watchlist.vehicle_id  LEFT JOIN vehicle_make make ON v.make=make.make_id LEFT JOIN vehicle_model model ON v.model=model.model_id  LEFT JOIN user_master user ON watchlist.set_by=user.user_id ORDER BY watchlist.to_date ASC");
            while($row=mysql_fetch_object($result))
            {
                $tmp[]=array(
                             "watch_id" => $row->watch_id,
                             "vehicle_id" => $row->vehicle_id,
                             "year" => $row->year,
                             "make" => $row->make,
                             "model" => $row->model_name,
                             "set_by" => $row->first_name." ".$row->last_name,
                             "set_date" => $row->set_date,
                             "to_date" => $row->to_date,
                             "remind_note" => $row->remind_note,
                             "due" => $row->due,
                             "duesoon"=> $row->duesoon,
							 "image_path" => "",
							"count_isread"=>0
                   
                            );
            }
            
             $reminderdata = array("reminder"=>$tmp);
		echo json_encode($reminderdata);
            
		/*$query = "SELECT `watch_id`,`vehicle_id`,`set_by`,`set_date_time`,
		`to_date`,`remind_note`,`first_name` FROM watchlist JOIN administrator on watchlist.set_by= administrator.admin_id ";
		$result = mysql_query($query) or die("Query ERROR".$query);
		$arrayCollection = new ArrayCollection();
        $reminderdata = array();
		$vresult = VehicleDAOImpl::getInstance()->getVehicleCount();       
        while ($row =  mysql_fetch_object($result))
        {
			$querymessage = "SELECT count(is_read) AS 'read'  FROM message where vehicle_id= ".$row->vehicle_id." AND is_read=0"; 
			$resultmessage=mysql_query($querymessage) or die($querymessage);
			$resultmessagecount= mysql_fetch_object($resultmessage);
			$queryimage = "SELECT `thump_url` FROM vehicle_image where vehicle_id= ".$row->vehicle_id; 
			$resultimage = mysql_query($queryimage) or die("Query ERROR");
			$image_path='';
			while ($row1 =  mysql_fetch_object($resultimage))
			{
				$image_path = $row1->thump_url;
				break;
			}
			$tmp[] = array(
			"watch_id" => $row->watch_id,
			"vehicle_id" => $row->vehicle_id,
			"set_by" => $row->set_by,
			"set_date" => $row->set_date_time,
	        "to_date" => $row->to_date,
			"remind_note" => $row->remind_note,
			"remind_by" => $row->first_name,
			"image_path" => $image_path,
			"count_isread"=>$resultmessagecount->read
			);
        }
        $reminderdata = array("reminder"=>$tmp,"countnum"=>$vresult);
		echo json_encode($reminderdata);*/	
            
            
	}
	if($action=='changestatus')
	{		
		$result =VehicleDAOImpl::getInstance()->updateVehicleFieldByID($_POST['vehicle_id'], 'status',"'".$_POST['status']."'", null, null,$_POST['adminid']);
		return $result;
	}
	if($action=='getreport')
	{
		$from=$_POST['from'];
		$to=$_POST['to'];
		$reportarray = array();
		$result = VehicleDAOImpl::getInstance()->getSubmissionReport($from, $to);
		if(isset($result->sub_array->source))
		{   
			$ar_reports = $result->sub_array->source;
			for($i=0; $i<count($ar_reports); $i++)
			{  
				$reportarray[] = array(
				"via_w_buyer" => $ar_reports[$i]->via_w_buyer,
				"via_m_buyer" => $ar_reports[$i]->via_m_buyer,
				"via_w_seller" => $ar_reports[$i]->via_w_seller,
				"via_m_seller" => $ar_reports[$i]->via_m_seller,
				"date" => $ar_reports[$i]->date,
				);
			}
			$totalreport=  array(
			"total_w_buyer" => $result->total_w_buyer,
			"total_m_buyer" => $result->total_m_buyer,
			"total_w_seller" => $result->total_w_seller,
			"total_m_seller" => $result->total_m_seller,
			);
		}		
        $report= array("reportarray"=>$reportarray,"total"=>$totalreport);	
	    echo json_encode($report);		
	}
	if($action=='getlocked')
	{
		$result = VehicleDAOImpl::getInstance()->getAllLockedVehicle();
		echo json_encode(array("lockedVehicles"=>$result->source));
		/**$lockvechicles = array();
	    //echo count($result->source[0]->vehicle_id);
		if(isset($result->source[0]->vehicle_id))
		{
			$ar_locked = $result->source;
			//echo count($ar_locked);
			for($i=0; $i<count($ar_locked); $i++)
			{  
				$lockvechicles[] = array(
				"vehicle_id" => $ar_locked[$i]->vehicle_id,
				"make" => $ar_locked[$i]->make,
				"model" => $ar_locked[$i]->model_name,
				"locked_by" => $ar_locked[$i]->locked_by,
				"locked_date" => $ar_locked[$i]->locked_date
				);
			}		
		}			
		$lockedv = array("lockedvehicles"=>$lockvechicles);       
        echo json_encode($lockedv);**/
	}
	if($action=='SharebyEmail')
	{
		$sharevo=new ShareVO();
		$sharevo->partner_id=$_POST['partner_id'];
		$sharevo->vehicle_id=$_POST['vehicle_id'];
		$sharevo->send_to=$_POST['send_to'];
		$sharevo->note=$_POST['note'];
		$sharevo->admin_id=$_POST['admin_id'];
		$sharevo->is_car_details=$_POST['is_car_details'];
		$sharevo->is_seller_details=$_POST['is_seller_details'];
		$sharevo->is_tow_details=$_POST['is_tow_details'];
		$sharevo->is_map_details=$_POST['is_map_details'];
		$sharevo->is_private_details=$_POST['is_private_details'];
		$sharevo->user_type=$_POST['user_type'];
		$result = VehicleDAOImpl::getInstance()->vehicleShareByEmail($sharevo);
		echo $result;
		//echo json_encode (array("result" => $result));
	}
	if($action=='SharebyUnknownEmail')
	{
		$sharevo->vehicle_id=$_POST['vehicle_id'];
		$sharevo->send_to=$_POST['send_to'];
		$sharevo->note=$_POST['note'];
		$sharevo->admin_id=$_POST['admin_id'];
		$sharevo->is_car_details=$_POST['is_car_details'];
		$sharevo->is_seller_details=$_POST['is_seller_details'];
		$sharevo->is_tow_details=$_POST['is_tow_details'];
		$sharevo->is_map_details=$_POST['is_map_details'];
		$sharevo->is_private_details=$_POST['is_private_details'];
		$sharevo->user_type=$_POST['user_type'];
		$result = VehicleDAOImpl::getInstance()->shareByUnknownEmail($sharevo);
		echo $result;
	}
	if($action=='getPartner')
	{
		$result = VehicleDAOImpl::getInstance()->getPartnerList();
		$ar_partnerlist = $result->source;
		$partners = array();
		for($i=0; $i<count($ar_partnerlist); $i++)
			{  
				$partners[] = array(
				"partner_id" => $ar_partnerlist[$i]->partner_id,
				"f_name" => $ar_partnerlist[$i]->f_name,
				"l_name" => $ar_partnerlist[$i]->l_name,
				"email" => $ar_partnerlist[$i]->email,
				"user_type" => $ar_partnerlist[$i]->user_type
				);
			}
		$partnersdata = array("partners"=>$partners);
		echo json_encode($partnersdata);		
	}
	if($action=='setWatchList')
	{
		$watch_list->vehicle_id=$_POST['vehicle_id'];
		$watch_list->to_date=$_POST['to_date'];
		$watch_list->remind_note=$_POST['remind_note'];
		$watch_list->isWatch=$_POST['isWatch'];
		$watch_list->remind_by=$_POST['remind_by'];
		$watch_list->set_by=$_POST['set_by'];
		$watch_list->isReminder=$_POST['isReminder'];
		$vresult = VehicleDAOImpl::getInstance()->setWatchList($watch_list);
	}	
	if($action=='getrembyid')
	{   
		$vid=$_POST['vehicle_id'];
		$vresult = VehicleDAOImpl::getInstance()->getReminderVO($vid);
		$vresultrem = array("vresult"=>$vresult);
		echo json_encode($vresultrem);	
	
	}
	if($action=='updateWatchList')
	{
		$watch_list->vehicle_id=$_POST['vehicle_id'];
		$watch_list->to_date=$_POST['to_date'];
		$watch_list->remind_note=$_POST['remind_note'];
		$watch_list->isWatch=$_POST['isWatch'];
		$watch_list->remind_by=$_POST['remind_by'];
		$watch_list->set_by=$_POST['set_by'];
		$watch_list->isReminder=$_POST['isReminder'];
		$watch_list->watch_id=$_POST['watchid'];	
		$vresult = VehicleDAOImpl::getInstance()->updateWatchList($watch_list);

	}	
	if($action=='removeWatchList')
	{
		$watch_list->vehicle_id=$_POST['vehicle_id'];
		$watch_list->isWatch=$_POST['isWatch'];
		$watch_list->remind_by=$_POST['remind_by'];
		$watch_list->isReminder=$_POST['isReminder'];
		$watch_list->watch_id=$_POST['watch_id'];	
		$result = VehicleDAOImpl::getInstance()->removeWatchlist($watch_list);
		return $result;

	}	
	if($action=='unlock')
	{
		$vehicle_id=$_POST['vehicle_id'];
		$vresult = VehicleDAOImpl::getInstance()->unlockVehicleByID($vehicle_id);
		echo json_encode(array( "status"=>$vresult ) );
	}
	if($action=='getcount')
	{
		$vresult = VehicleDAOImpl::getInstance()->getVehicleCount();
		$countdata = array("countnum"=>$vresult);
		echo json_encode($countdata);
	}
	if($action=='addPnotes')
	{
		$mesvo=new MessageVO();
		$mesvo->message=$_POST['message'];
		$mesvo->messageOwener=$_POST['messageOwener'];
		$mesvo->messageRecipient=$_POST['messageRecipient'];
		$mesvo->vehicleId=$_POST['vehicleId'];
		$mesvo->userType=$_POST['userType'];
		$mesvo->offer=0;
		$mesvo->messageTo=$_POST['messageTo'];
		$mesvo->isRead=$_POST['isRead'];
		$result = VehicleDAOImpl::getInstance()->addNewPrivateNote($mesvo);
		echo $result;
	}
	if($action=='addMessages')
	{
		$mesvo=new MessageVO();
		$mesvo->message=$_POST['message'];
		$mesvo->messageOwener=$_POST['messageOwener'];
		$mesvo->messageRecipient=$_POST['messageRecipient'];
		$mesvo->vehicleId=$_POST['vehicleId'];
		$mesvo->userType=$_POST['userType'];
		$mesvo->offer=0;
		$mesvo->messageTo=$_POST['messageTo'];
		$mesvo->isRead=$_POST['isRead'];
		$result = VehicleDAOImpl::getInstance()->updateMessageVehicleID($mesvo, '', 'reply from buyer', 'false', '');
		if($result)
		{
			$message = VehicleDAOImpl::getInstance()->getAllMessageVehicleID($mesvo->vehicleId) ;
			$data = encodeJsonMessageString($message);
			echo $data;
		}
	//return $result;
	}
	function encodeJsonMessageString($message)
	{
		$ar_message = $message->source;
		$messages = array();
		for($i=0; $i<count($ar_message); $i++)
		{    
					
			$messages[] = array(
			"vehicleId" => $ar_message[$i]->vehicleId,
			"message" => $ar_message[$i]->message,
			"messageOwener" => $ar_message[$i]->messageOwener,
			"messageRecipient" => $ar_message[$i]->messageRecipient,
			"offer" => $ar_message[$i]->offer,
			"isRead" => $ar_message[$i]->isRead,
			"dateTime" => $ar_message[$i]->dateTime);
		}
			$messagedata = array("message"=>$messages);
			return json_encode($messagedata);
	}	
	if($action=='getMessages')
	{
		$vid=$_POST['vid'];
		$up=VehicleDAOImpl::getInstance()->getAllMessageVehicleID($vid);
		$data = encodeJsonMessageString($up);
		echo $data;
	}
	if($action=='getPnotes')
	{
		$vid=$_POST['vid'];
		$up=VehicleDAOImpl::getInstance()->getAllPrivateNote($vid);
		echo json_encode($up->source);
	}
	if($action=='getvbyid')
	{
		$vid=$_POST['veid'];
		$adminName=$_POST['adminname'];
		$adminId=$_POST['adminid'];
		$up=VehicleDAOImpl::getInstance()->confirmVehicleLockByID($vid, $adminName,$adminId);
		$result = VehicleDAOImpl::getInstance()->getVehicleByVehicleID($vid);
		if($up=='open')
		{
			$vehiclesingle=encodeJsonString($result);
			echo json_encode(array("vsingle"=>$vehiclesingle,"result"=>$up));
		}
		else
		{
			echo json_encode(array("result"=>$up));
		}
	}
        
        if($action=='checkVLocked')
        {
           echo  json_encode(array("status" => VehicleDAOImpl::getInstance()->checkVehicleLocked($_POST['veid'])));
        }
        
        if($action=='setTowStatus')
        {
            if(VehicleDAOImpl::getInstance()->setTowStatus($_POST["vehicle_id"],$_POST["tow_status"]))

              echo 1;
           else
               echo 0;
        }

	if($action=='getvehicle')
	{
		$allvehicle=new VehicleRequestVO();

			$allvehicle->criteria= $_POST['criteria'];
			$allvehicle->search_key=$_POST['search_key'];
				$allvehicle->vehicle_count= $_POST['vehicle_count'];
                         if(isset($_POST['type']))
                          {
                            $allvehicle->type=$_POST['type'];
                            }
                         else {
                                $allvehicle->type='';
                        }
                        $allvehicle->status=$_POST['status'];
                        if($allvehicle->type=="tower")
                                   $allvehicle->tower_company = $_POST['tower_company'];
                         else    
                                     $allvehicle->tower_company = '';
				 $allvehicle->num_record= $_POST['num_record'];
				 $allvehicle->index= $_POST['index'];
				   $allvehicle->sort_string= $_POST['sort_string'];
				   $allvehicle->sort_type=$_POST['sort_type'];
	  $result_allvehicle = VehicleDAOImpl::getInstance()->getVehicleByPageIndex($allvehicle);

	$vehiclelist = array();
	foreach ($result_allvehicle->seller->source as $value)
	{
		if(isset($value->vehicle_id))
		{
			$vehiclelist[]=encodeJsonString($value);
		}
	}
	$count=$result_allvehicle->vehicle_count;
	$avg_askPrice=$result_allvehicle->avg_askPrice;
    $avg_bidPrice=$result_allvehicle->avg_bidPrice;
    $avg_year=$result_allvehicle->avg_year;
    $total_askPrice=$result_allvehicle->total_askPrice;
    $total_bidPrice=$result_allvehicle->total_bidPrice;
    $unreadMsg=$result_allvehicle->unreadMsg;
    echo json_encode(array("vehicleslist"=>$vehiclelist,"count"=>$count,"avg_askPrice"=>$avg_askPrice,"avg_bidPrice"=>$avg_bidPrice,"avg_year"=>$avg_year,"total_askPrice"=>$total_askPrice,"unreadMsg"=>$unreadMsg));
	}
	function encodeJsonString($vehicle)
	{  
		$reminder_1 = array();
		if($vehicle->reminder)
		{  
			$reminder_1[]=array("watchid"=> $vehicle->reminder->watch_id,
			 "vehicle_id"=> $vehicle->reminder->vehicle_id,
			 "set_by"=> $vehicle->reminder->set_by,
			 "set_date_time"=> $vehicle->reminder->set_date,
			 "to_date"=> $vehicle->reminder->to_date,
			 "remind_note"=> $vehicle->reminder->remind_note,
			 "first_name"=> $vehicle->reminder->remind_by
			 );
		}
		$ar_images = $vehicle->image_collection->source;
		$images = array();
		for($i=0; $i<count($ar_images); $i++)
		{
			$images[] = array(
			"imageId"=>$ar_images[$i]->imageId,
			"imageUrl"=>$ar_images[$i]->imageUrl,
			"cropUrl"=>$ar_images[$i]->cropUrl,
			"thumpUrl"=>$ar_images[$i]->thumpUrl);
		}
		
		$ar_message = $vehicle->messages->source;
		$messages = array();
		for($i=0; $i<count($ar_message); $i++)
		{
				
			if($ar_message[$i]!='')
			{
				$messages[] = array(
				"vehicleId" => $ar_message[$i]->vehicleId,
				"message" => $ar_message[$i]->message,
				"messageOwener" => $ar_message[$i]->messageOwener,
				"messageRecipient" => $ar_message[$i]->messageRecipient,
				"offer" => $ar_message[$i]->offer,
				"isRead" => $ar_message[$i]->isRead,
				"dateTime" => $ar_message[$i]->dateTime);
			}
		}				
		$ar_privatenotes = $vehicle->privateNote_collection->source;
		$pnotes = array();
		for($i=0; $i<count($ar_privatenotes); $i++)
		{
				
			if($ar_privatenotes[$i]!='')
			{
				$pnotes[] = array(
				"vehicleId" => $ar_privatenotes[$i]->vehicleId,
				"message" => $ar_privatenotes[$i]->message,
				"messageOwener" => $ar_privatenotes[$i]->messageOwener,
				"messageRecipient" => $ar_privatenotes[$i]->messageRecipient,
				"offer" => $ar_privatenotes[$i]->offer,
				"isRead" => $ar_privatenotes[$i]->isRead,
				"dateTime" => $ar_privatenotes[$i]->dateTime);
			}
		}			
		$vehicles[] = array(		
		"vehicle_id" => $vehicle->vehicle_id,
		"year"=> $vehicle->year,
		"make"=> $vehicle->make,
		"make_name"=> $vehicle->make_name,
		"model_name"=> $vehicle->model_name,
		"state_name"=>$vehicle->state_name,
		"v_state_name"=>$vehicle->v_state_name,
		"model"=> $vehicle->model,
		"vin"=> $vehicle->vin,
		"mileage"=> $vehicle->mileage,
		"engine"=> $vehicle->engine,
		"transmission"=> $vehicle->transmission,
		"drivetrain_type"=> $vehicle->drivetrain_type,
		"title_note"=> $vehicle->title_note,
		"wrong"=> $vehicle->wrong,
		"great"=> $vehicle->great,
		"i_want_to"=> $vehicle->i_want_to,
		"bid_price"=> $vehicle->bid_price,
		"asking_price"=> $vehicle->asking_price,
		"accepted_price"=> $vehicle->accepted_price,
		"removal_date"=> $vehicle->removal_date,
		"first_name"=> $vehicle->first_name,
		"email"=> $vehicle->email,
		"phone"=> $vehicle->phone,
		"cell_phone"=> $vehicle->cell_phone,
		"contact_me"=> $vehicle->contact_me,
		"located_at"=> $vehicle->located_at,
		"paper_work"=> $vehicle->paper_work,
		"street_address"=> $vehicle->street_address,
		"city"=> $vehicle->city,
		"state"=> $vehicle->state,
		"zip"=> $vehicle->zip,
		"v_street_address"=> $vehicle->v_street_address,
		"v_city"=> $vehicle->v_city,
		"v_state"=> $vehicle->v_state,
		"v_zip"=> $vehicle->v_zip,
		"contact_name"=> $vehicle->contact_name,
		"contact_phone"=> $vehicle->contact_phone,
		"delivery"=> $vehicle->delivery,
		"delivery_note"=> $vehicle->delivery_note,
		"delivery_option"=> $vehicle->delivery_option,
		"is_read"=>$vehicle->is_read,
		"is_watch"=>$vehicle->is_watch,
		"private_note"=> $vehicle->private_note,
		"contact_note"=> $vehicle->contact_note,	   
		"status"=> $vehicle->status,
		"current_action"=> $vehicle->current_action,
		"update_date"=> $vehicle->update_date,
		"created_date"=>$vehicle->date,	
        "created_by"=>$vehicle->created_by,
		"title_name"=> $vehicle->title_name,
		"ad_source"=> $vehicle->ad_source,
		"color"=> $vehicle->color,
		"bussiness_name"=> $vehicle->bussiness_name,
		"assist_by"=>$vehicle->assist_by,
		"sub_ip"=>$vehicle->sub_report->sub_ip,
		"sub_platform"=>$vehicle->sub_report->sub_platform,
		"sub_device"=>$vehicle->sub_report->sub_device,
		"date"=>$vehicle->sub_report->date,
		"user_type"=>$vehicle->sub_report->user_type,
                 "tow_status_name"=>$vehicle->tow_status_name,        
                "tow_is_read"=>$vehicle->tow_is_read,    
                "tower_street" => $vehicle->tower_street,
                "tower_city" => $vehicle->tower_city,
                "tower_state" => $vehicle->tower_state,
                    
                "vehicle_role" => $vehicle->vehicle_role,    
                    
		"messages"=> $messages, 
		"images"=> $images,
		"pnotes"=> $pnotes ,
		"reminder_1"=>$reminder_1
		);
		$vehicledata = array("vehicle"=>$vehicles);
		return $vehicledata ;
	}
	if($action=='update')
	{
		$object=new VehicleVO();
		$object->adminUser->firstName=$_POST['fadminname'];
		$object->adminUser->admin_id=$_POST['admin_id'];
		$object->vehicle_id = $_POST['vehicle_id'];
		$object->year = $_POST['year'];
		$object->make=$_POST['make'];
		$object->model=$_POST['model'];
		$object->vin=$_POST['vin'];
		$object->mileage=$_POST['mileage'];
		$object->engine=$_POST['engine'];
		$object->transmission=$_POST['transmission'];
		$object->drivetrain_type=$_POST['drivetrain_type'];
	    $object->title_note=$_POST['title_note'];
	    $object->title_name=$_POST['title_name'];
	    $object->ad_source=$_POST['ad_source'];
		$object->wrong=$_POST['wrong'];
		$object->great=$_POST['great'];
		$object->paper_work=$_POST['paper_work'];		
		$object->i_want_to=$_POST['i_want_to'];
		$object->first_name=$_POST['first_name'];
		$object->bussiness_name=$_POST['bussiness_name'];
		$object->contact_name=$_POST['contact_name'];
        $object->street_address=$_POST['street_address'];
		$object->city=$_POST['city'];
		$object->state=$_POST['state'];
		$object->zip=$_POST['zip'];
		$object->phone=$_POST['phone'];
		$object->cell_phone=$_POST['cell_phone'];
		$object->email=$_POST['email'];
		$object->contact_me=$_POST['contact_me'];
		$object->contact_note=$_POST['contact_note'];
        $object->delivery=$_POST['delivery'];
		$object->removal_date=$_POST['date'];
		$object->color=$_POST['color'];
		$object->delivery_note=$_POST['delivery_note'];
		$object->accepted_price=$_POST['accepted_price'];
		$object->asking_price=$_POST['asking_price'];
		$object->bid_price=$_POST['bid_price'];
		 $object->v_street_address=$_POST['v_street_address'];
		$object->v_city=$_POST['v_city'];
		$object->v_state=$_POST['v_state'];
		$object->v_zip=$_POST['v_zip'];
		//$object->located_at=$_POST['located_at'];
		$object->i_want_to=$_POST['i_want_to'];
		$object->is_watch=$_POST['is_watch'];
		$object->is_read=1;		
		$object->status=$_POST['status'];
		$object->current_action=$_POST['status'];
		$object->is_partner=0;	     
		updateVehicle($object);
	}		
	function updateVehicle($vehicle) 
	{   
		$isNormal=$_POST['isNormal'];
		$result = VehicleDAOImpl::getInstance()->updateVehicleByVehicleID($vehicle, $isNormal);
$resultmessage = VehicleDAOImpl::getInstance()->updateVehicleFieldByID($vehicle->vehicle_id,'is_read',$vehicle->is_read,null,null, $_POST['fadminname']);				
		$data = array("data"=>$result);
		echo json_encode($data);	
		/* $vresult = VehicleDAOImpl::getInstance()->getVehicleCount();
		$countdata = array("countnum"=>$vresult);
		echo json_encode($countdata);*/
	}
	function encodeJsonStringMedaiator($vehicle)
    {
        
		$ar_images = $vehicle->image_collection->source;
		$images = array();
		for($i=0; $i<count($ar_images); $i++)
		{
			$images[] = array(
			"imageId"=>$ar_images[$i]->imageId,
			"imageUrl"=>$ar_images[$i]->imageUrl,
			"cropUrl"=>$ar_images[$i]->cropUrl,
			"thumpUrl"=>$ar_images[$i]->thumpUrl);
		}		
		$ar_message = $vehicle->messages->source;
		$messages = array();
		for($i=0; $i<count($ar_message); $i++)
		{				
			if($ar_message[$i]!='')
			{
				$messages[] = array(
				"vehicleId" => $ar_message[$i]->vehicleId,
				"message" => $ar_message[$i]->message,
				"messageOwener" => $ar_message[$i]->messageOwener,
				"messageRecipient" => $ar_message[$i]->messageRecipient,
				"offer" => $ar_message[$i]->offer,
				"isRead" => $ar_message[$i]->isRead,
				"dateTime" => $ar_message[$i]->dateTime);
			}
		}
		$partnerEmail = '';
		$partnerID = '';
		$partnerFName = '';
		if($vehicle->partner_details != '')
		{
			if($vehicle->partner_details->email != null || $vehicle->partner_details->email != '')
			{
				$partnerEmail = $vehicle->partner_details->email;
				$partnerID = $vehicle->partner_details->partner_id;
				$partnerFName = $vehicle->partner_details->f_name;
			}
		}		
		$vehicles = array(		
		"vehicle_id" => ($vehicle->vehicle_id)?:'',
        "year"=> ($vehicle->year)?:'',
        "make"=> ($vehicle->make)?:'',
        "model"=> ($vehicle->model)?:'',
        "vin"=> ($vehicle->vin)?:'',
        "mileage"=> ($vehicle->mileage)?:'',
        "engine"=> ($vehicle->engine)?:'',
        "transmission"=> ($vehicle->transmission)?:'',
        "drivetrain_type"=> ($vehicle->drivetrain_type)?:'',
        "title_note"=> ($vehicle->title_note)?:'',
        "wrong"=> ($vehicle->wrong)?:'',
        "great"=> ($vehicle->great)?:'',
        "i_want_to"=> ($vehicle->i_want_to)?:'',
        "bid_price"=> ($vehicle->bid_price)?:'',
        "asking_price"=> ($vehicle->asking_price)?:'',
        "accepted_price"=> ($vehicle->accepted_price)?:'',
        "removal_date"=> ($vehicle->removal_date)?:'',
        "first_name"=> ($vehicle->first_name)?:'',
        "email"=> ($vehicle->email)?:'',
        "phone"=> ($vehicle->phone)?:'',
        "cell_phone"=> ($vehicle->cell_phone)?:'',
        "contact_me"=> ($vehicle->contact_me)?:'',
        "located_at"=> ($vehicle->located_at)?:'',
        "paper_work"=> ($vehicle->paper_work)?:'',
        "street_address"=> ($vehicle->street_address)?:'',
        "city"=> ($vehicle->city)?:'',
        "state"=> ($vehicle->state)?:'',
        "zip"=> ($vehicle->zip)?:'',
        "v_street_address"=> ($vehicle->v_street_address)?:'',
        "v_city"=> ($vehicle->v_city)?:'',
        "v_state"=> ($vehicle->v_state)?:'',
        "v_zip"=> ($vehicle->v_zip)?:'',
        "contact_name"=> ($vehicle->contact_name)?:'',
        "contact_phone"=> ($vehicle->contact_phone)?:'',
        "delivery"=> ($vehicle->delivery)?:'',
        "delivery_note"=> ($vehicle->delivery_note)?:'',
        "delivery_option"=> ($vehicle->delivery_option)?:'',   
        "private_note"=> ($vehicle->private_note)?:'',
        "contact_note"=> ($vehicle->contact_note)?:'',   
        "status"=> ($vehicle->status)?:'',
        "current_action"=> ($vehicle->current_action)?:'',
        "update_date"=> ($vehicle->update_date)?:'',
        "date"=> ($vehicle->date)?:'',             
        "title_name"=> ($vehicle->title_name)?:'',
        "ad_source"=> ($vehicle->ad_source)?:'',
        "color"=> ($vehicle->color)?:'',
		"partner_email"=>($partnerEmail)?:'',
		"partner_name" =>($partnerFName)?:'',
		"partner_id"=>($partnerID)?:'',
        "bussiness_name"=> ($vehicle->bussiness_name)?:'',
		"messages"=> $messages, 
		"images"=> $images 
        );
	   $vehicledata = array("vehicle"=>$vehicles);
       return json_encode($vehicledata);
    }
?>